//
//  NetworkingSevers.m
//  NetworkingSevers
//
//  Created by WorkSpace on 4/1/14.
//
//

#import "NetworkingSevers.h"

@implementation NetworkingSevers
//@dynamic baseclient;
//@synthesize delegate;
- (id)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
- (void)setRequestStyle:(NSString*)style
       andSetPathString:(NSString*)path
       andSetHeadString:(NSString*)head
          andActionName:(NSString*)action
   andSetMainDictionary:(NSMutableDictionary*)dictionary
                  block:(void(^)(id json))processResult
              failblock:(void(^)(NSError *error))failresult
{
    //URL
    baseclient = [BaseHttpClient sharedBaseHttpClientUrl:[self returnTheUrlTop:action]];
    //Request Style
    if ([style isEqualToString:@"POST"]||style==nil)
    {
        [baseclient postDataWithPath:path
                          withAction:action
                      WithParameters:dictionary
                               block:^(id JSON)
                                {
                                    id dellData = [self toWriteDataStyles:JSON];
                                    if (dellData==nil)
                                    {
                                        UIAlertView *noDataRequest = [[UIAlertView alloc]initWithTitle:@"错误" message:@"网络异常无法获取数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                        [noDataRequest show];
                                        return ;
                                    }
                                    processResult(dellData);
                                }
                                failblock:^(NSError *error)
                                {
                                   failresult(error);
                                }];
    }
}
-(NSString*)returnTheUrlTop:(NSString*)actionUrl
{
    self.httpUrl = @"http://localhost:8888/wordpress/";
    NSMutableString *reUrlString = [[NSMutableString alloc]init];
    if (actionUrl!=nil)
    {
        self.actionUrl = actionUrl;
        [reUrlString appendString:self.httpUrl];
        [reUrlString appendString:self.actionUrl];
        LSLog(@"URL = %@",reUrlString);
        return reUrlString;
    }
    reUrlString = [NSMutableString stringWithString:self.httpUrl];
    NSLog(@"URL = %@",reUrlString);
    return reUrlString;
}
#pragma mark 字典-》JsonString
// 将字典或者数组转化为JSON串
- (NSData *)toJSONData:(id)theData{
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil)
    {
        return jsonData;
    }
    else
    {
        return nil;
    }
}
- (id)toWriteDataStyles:(id)json
{
    if ([json length] > 0)
    {
        NSError *error = nil;
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:json options:NSJSONReadingAllowFragments
                         error:&error];
        //转换方法
        if (jsonObject != nil )
        {
            //如果jsonObject是字典类
            if ([jsonObject isKindOfClass:[NSDictionary class]])
            {
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
//                NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
                return deserializedDictionary;
            }
            //如果jsonObject是数组类
            else if ([jsonObject isKindOfClass:[NSArray class]])
            {
                NSArray *deserializedArray = (NSArray *)jsonObject;
//                NSLog(@"Dersialized JSON Array = %@", deserializedArray);
                return deserializedArray;
            }
            else
            {
//                NSLog(@"I can't deal with it");
            }
        }
    }
    return nil;
}
@end
